/* Coded for sigMatch
 * Author: Ramakrishnan Kandhan (ramak@cs.wisc.edu)
 *
 * ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 * Portions of this software are based upon public domain software
 * originally written at the National Center for Supercomputing Applications,
 * University of Illinois, Urbana-Champaign.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string>
#include "bloom_filter.h"
using namespace  std;
namespace sigMatch {

#define SIZE_IND 256
#define SIZE_WORDS 4
#define NUM_INDICES 256
#define BUFF_SIZE 1024

int rid, lid, i;
int BLOOM_SIZE;
bool bcount[SIZE_IND][SIZE_IND];

typedef struct sigLeaf
{
	struct bloomf* bf[SIZE_IND];
}sigLeaf;

void initsigLeaf(struct sigLeaf* l)
{
	int y=0;
	for (y=0;y<SIZE_IND;y++)
			l->bf[y] = NULL;
}

typedef struct sigRoot
{
	struct sigLeaf* nodes[SIZE_IND];
} sigRoot;

void  initsigRoot(struct sigRoot* s)
{
	int y=0;
	for (y=0;y<SIZE_IND;y++)
			s->nodes[y] = NULL;

}

struct bloomf* createbloomf (char* fname, unsigned int l)
{
	FILE* fp = fopen(fname,"r");
	if (!fp)
	{
		printf("Cannot open file %s lol\n",fname);
		exit(0);
	}

	struct bloomf* bloom = (bloomf*) malloc (sizeof(bloomf));
	initbloomf(bloom, BLOOM_SIZE);
	char* s = (char*) malloc (sizeof(char)*l);
	int ind; int i;
	char* cstr = (char*) malloc (sizeof(char)*l/2);
	while (!feof(fp))
	{
		fscanf(fp,"%s",s);
		for (i=0; i<l; i+=2)
		{
			char t[3];
			t[0] = s[i]; t[1] = s[i+1];t[2] ='\0';

			sscanf (t,"%x",&ind);     
			cstr[i/2] = (char) ind;
		}
		insertEntry(bloom,cstr, (int) l/2);

	}
	fclose(fp);
        free(s);
        free(cstr);
	return bloom;
}


struct sigRoot* createTree(char * fname, int length, char * dirname)
{
 	struct sigRoot* root;
	FILE* fd = fopen (fname,"r");
	if (!fd)
	{
		printf("Cannot open file %s\n",fname);
		exit(-1);
	}
	root = (sigRoot*) malloc (sizeof(sigRoot));
	initsigRoot(root);
	int ind,ind1;

	while(!feof(fd))
	{
		char word[SIZE_WORDS+1];

		fscanf(fd,"%s\n",word);
		char tm[3]; tm[0]=word[0]; tm[1]= word[1]; tm[2] ='\0';
		sscanf(tm,"%x",&ind);
		struct sigLeaf* temp = (sigLeaf*) malloc(sizeof(sigLeaf));

		initsigLeaf(temp);
		char fn [SIZE_WORDS+20];

		sprintf(fn, "%s/%c%c%c%c.txt",dirname,word[0],word[1],word[2],word[3]);

		struct bloomf* b = createbloomf(fn,length);

		tm[0] = word[2]; tm[1] = word[3]; tm[2] ='\0';
		sscanf(tm,"%x",&ind1);
		if(root->nodes[ind] == NULL)
			root->nodes[ind] = temp;

		root->nodes[ind]->bf[ind1] = b;

	}

  int i1,i2;
	for (i1=0; i1<SIZE_IND; i1++) {
		for (i2=0; i2<SIZE_IND; i2++) {
			bcount[i1][i2] = 0;
		}
  }

	for (i1=0;i1<SIZE_IND;i1++)
		if (root->nodes[i1] != NULL)
			for (i2=0; i2<SIZE_IND; i2++)
				if (root->nodes[i1]->bf[i2] != NULL)
					bcount[i1][i2] = 1;

	fclose(fd);
	return root;
}

int sigScanbuff(struct sigRoot* root,unsigned char *buffer, int length, int slength)
{
	i = 2;
	if (length <= slength+2)
		return 1;
	rid = (int) (buffer[i-2]);
	while (i<length) {
		lid = (int) (buffer[i-1]);
		if (bcount[rid][lid]) {
			if (length-i <=slength)
				break;
			if (bloomcontains(root->nodes[rid]->bf[lid],(char*) buffer+i, slength))
					return 0;
		}
		i++;
		rid = lid;
	}
	return 1;
}

int Destroy(sigRoot *root)
 { 
   int i=0;
   int j=0;
   for(i=0;i<SIZE_IND;i++)
     for(j=0;j<SIZE_IND;i++)
       if(NULL!=root->nodes[i]->bf[j]){
         free(root->nodes[i]->bf[j]); 
         root->nodes[i]->bf[j]=NULL;
       }  
   
   return 1;
 }



}
